package cn.edu.eight.food.controller;


import cn.edu.eight.food.model.OfUser;
import cn.edu.eight.food.service.ofUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/login")
public class ofLoginController {
    @Autowired
    ofUserService ofUserService;

    @RequestMapping("/toAdmin")
    private String toAdminLogin(){
        return "redirect:/manage/page/login.html";
    }

    @RequestMapping("admin")
    private String adminLogin(@RequestParam("username") String username,
                              @RequestParam("password") String password,
                              @RequestParam("captcha") String captcha,
                              HttpSession session){
        if (StringUtils.isEmpty(captcha)) {
            session.setAttribute("errorMsg", "验证码不能为空");
            return "redirect:/login/toAdmin";
        }
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            session.setAttribute("errorMsg", "用户名或密码不能为空");
            return "redirect:/login/toAdmin";
        }
        OfUser ofUser =ofUserService.adminLogin(username,password);
        if (ofUser != null){
            session.setAttribute("loginUsername",ofUser.getOfUserName());
            session.setAttribute("loginUserId",ofUser.getOfUserId());
            //session过期时间设置为7200秒 即两小时
            session.setMaxInactiveInterval(60 * 60 * 2);
            return "redirect:/manage/index.html";
        }else {
            session.setAttribute("errorMsg","账号或者密码错误");
            return "redirect:/login/toAdmin";
        }
    }

    @RequestMapping("/toRegister")
    private String toWelcome(){
        return "register";
    }

    @RequestMapping("/toUserLogin")
    private String toUserLogin(){
        return "login";
    }

    @RequestMapping("/userLogin")
    private String userLogin(@RequestParam("username") String username,
                              @RequestParam("password") String password,
                             HttpSession session,
                             HttpServletRequest request){
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            session.setAttribute("errorMsg", "用户名或密码不能为空");
            return "redirect:/login/toUserLogin";
        }
        OfUser ofUser =ofUserService.userLogin(username,password);
        if (ofUser != null){
            session.setAttribute("loginUser",ofUser);
            //session过期时间设置为7200秒 即两小时
            session.setMaxInactiveInterval(60 * 60 * 2);
            return "redirect:/index/food";
        }else {
            session.setAttribute("errorMsg","账号或者密码错误");
            return "redirect:/login/toUserLogin";
        }
    }

    @RequestMapping("/exitLogin")
    private String exitLogin(HttpServletRequest req){
        req.getSession().removeAttribute("loginUser");
        return "redirect:/index/food";
    }
}
